import utils.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/16 16:08
 * @description:
 */
public class Solution257 {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        backtracking(root, new LinkedList<>(), res);
        return res;

    }

    public void backtracking(TreeNode root, LinkedList<String> path, List<String> res) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            path.add(root.val + "");
            res.add(String.join("->", path));
            path.removeLast();

        } else {
            path.add(root.val + "");
            backtracking(root.left, path, res);
            backtracking(root.right, path, res);
            path.removeLast();
        }
    }

    public static void main(String[] args) {
        Solution257 solution = new Solution257();
        TreeNode root = TreeNode.buildTree(new int[]{1, 2, 3, -1, 5});
        TreeNode.show(root);
        System.out.println(solution.binaryTreePaths(root));
    }
}
